import { Component, OnInit } from '@angular/core';
import {FormControl, FormGroupDirective, NgForm, Validators} from '@angular/forms';
import {ErrorStateMatcher} from '@angular/material/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import {LoginAndregisterService} from '../login-andregister.service';
import { Router} from '@angular/router'; //导入router服务
@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})

export class RegisterComponent implements OnInit {

  constructor(private message: NzMessageService,private loginAndregisterService:LoginAndregisterService,private router: Router) { }
  email='';
  passWd='';
  resurePassWd='';
  isVisible=false;
  judge(){
    if(this.email=='' || this.passWd=='' || this.resurePassWd==''){
      this.message.create('error', `注册信息不能为空！`);
    }else if(this.passWd!=this.resurePassWd){
      this.message.create('error', `密码和确认密码不一致！`);
    }else{
      this.isVisible=true;
    }
  }
  handleCancel(){
    this.isVisible=false;
  }
  handleOk(){
    this.register();
    this.isVisible=false;
  }
  register(){
    console.log("email:",this.email);
    console.log("pass:",this.passWd);
    console.log("resure:",this.resurePassWd);
    this.loginAndregisterService.submitRegister(this.email,this.passWd).subscribe(res =>
      {
        console.log("注册结果：",res)
        if(res.code==0){
            this.message.create('success',res.data);
            //this.router.navigateByUrl('login');
            this.login(this.email,this.passWd);
        }else {
            this.message.create('success',res.msg)
        }
      }
    );
  }
  login(email:String,password:String){
    this.loginAndregisterService.submitLogin(email,password).subscribe(res=>{
      if(res.code==0){
        this.message.create('success',res.data);
        window.localStorage['username']=email;
        window.localStorage['password']=password;
        this.router.navigateByUrl('home/interest');
      }else{
        //自动登录失败
        this.message.create('error',res.msg);
      }
    })
  }
  ngOnInit(): void {

  }

}
